/**
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.gitee.huangjianbo.supos.openapi.core.http;

import io.gitee.huangjianbo.supos.openapi.core.errors.ApiClientException;
import io.gitee.huangjianbo.supos.openapi.core.tools.StringUtils;

/**
 * 请求路径
 *
 * @author tomcat
 * @version 1.0
 */
public class HttpPath {

    /**
     * HTTP请求方法（默认GET）
     * <p>
     * 派生类通过构造函数可指定特定方法，如{@link HttpGetPath}
     */
    private HttpMethod method = HttpMethod.GET;

    /**
     * 标准化请求路径
     * <p>
     * 存储原始路径值，通过{@link #toString()}方法进行标准化输出
     */
    private final String path;

    /**
     * 受保护构造函数（供派生类使用）
     * 
     * @param path 请求路径（支持相对路径，如"api/v1/users"）
     */
    protected HttpPath(String path) {
        this.path = path;
    }

    /**
     * 方法指定构造函数（供派生类扩展）
     * 
     * @param path   请求路径
     * @param method 指定HTTP方法
     */
    protected HttpPath(String path, HttpMethod method) {
        this.path = path;
        this.method = method;
    }

    public static HttpPath of(String path) {
        if (StringUtils.isEmpty(path)) {
            throw new ApiClientException("the http path must not be empty", new NullPointerException());
        }
        return new HttpPath(path);
    }

    public static HttpPath of(String path, HttpMethod method) {
        if (StringUtils.isEmpty(path)) {
            throw new ApiClientException("the http path must not be empty", new NullPointerException());
        }
        if (method == null) {
            throw new ApiClientException("the http method must not be null", new NullPointerException());
        }
        return new HttpPath(path, method);
    }

    /**
     * 获取标准化请求路径
     * 
     * @return 以斜杠开头的完整路径（示例："/api/v1/users"）
     */
    @Override
    public String toString() {
        return this.path.startsWith("/") ? this.path : "/" + this.path;
    }

    /**
     * 获取HTTP请求方法
     * 
     * @return 关联的HTTP方法，派生类可覆盖默认值
     */
    public HttpMethod getMethod() {
        return method;
    }

    /**
     * 获取原始路径值
     * 
     * @return 未经处理的原始路径输入
     */
    public String getPath() {
        return path;
    }
}
